<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理员维护</title>

<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
 
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
<script>
// 新增
function add(){
	$(":checked").attr("checked",false);
	document.formName.reset();
	$("#id").val("");
    dia("新增管理员");
}

//编辑管理员
 function edit(id,name,roleids){
	$(":checkbox").attr("checked",false);
	
	$("#name").val(name);
	$("#id").val(id);
	if(roleids!=""){
		if(roleids.indexOf(",")!=-1){
			var idattr = roleids.split(",");
			for(var rid in idattr){
				document.getElementById("role_"+idattr[rid]).checked = true;
			}
		}else{
				document.getElementById("role_"+roleids).checked = true;
		}
	}
	dia("编辑管理员");
} 
function dia(title){//弹窗
   dialog = art.dialog({
        title: title,
        width:500,
        lock:true,
        content: document.getElementById('create')
    });
}

function del(id){
	art.dialog.confirm('是否确认删除' , function(){
		$.ajax({
			   type: "get",
			   dataType:"json",
			   url: "/admin/admin_del.do",
			   data:{id:id},
			   beforeSend:function(){
				    art.dialog.tips("删除中...");
			   },
			   success: function(data){//成功的时候调用
				   if(data.success){
					   location.reload();
				   }else{
					   error_msg(data.msg , function(){location.reload();});
				   }
			   }
		});
	})
}



function sub(){
	$(this).attr("disabled","disabled");
	if($("#signupForm").valid()){
	$.ajax({
		   type: "POST",
		   dataType:"json",
		   url: "/admin/admin_add.do",
		   data:$("form").serialize(), //序列表表格内容为字符串
		   beforeSend:function(){
			   art.dialog.tips("加载中.....");
		   },
		   success: function(data){//成功的时候调用
			   if(data.success){
				   location.reload();
			   }
		   }
	});
	
};
}
$().ready(function(){
    $("#signupForm").validate({
    	rules:{
    		name:{
    			required:true,
    			maxlength:10
    		}
    	}
    });
});
</script>
</head>
<body>

	<div class="panel panel-info">
		<div class="panel-heading">
		
       [#assign canedit = false /]
        [@func_perm url="/admin/admin_add.do"  ]
        [#assign canedit = true /]
			  <button type="button"  onclick="add();" class="btn btn-info"><span class="glyphicon glyphicon-plus"></span>&nbsp;新增</button>
         [/@func_perm ]
		</div>
		<table class="table table-hover table-bordered">

			<tr>
				<th width="50px">#</th>
				<th>名称</th>
				<th>角色</th>
				<th width="150">操作</th>
			</tr>
			 [#assign candel=false /]
        [@func_perm  url="/admin/admin_del.do" ]
            [#assign candel=true /]
        [/@func_perm]
        	
			[#list admins as admin]
			<tr>
				<td>${admin_index+1}</td>
				<td>${admin.name!}</td>
				<td>
				${admin.rolenames!}
				</td>
				<td >
				[#if canedit]
				<input value="编辑" type="button" onclick="edit(${admin.id},'${admin.name!}','${admin.roleids!}');" class="btn btn-primary btn-xs"/> 
				[/#if]
				[#if candel]
				<input value="删除" type="button" onclick="del(${admin.id});" class="btn btn-danger btn-xs" /></td>
				[/#if]	
			</tr>
			[/#list]
		</table>

	</div>

	<!--新增管理员 -->

	<div id="create" style="display: none;" >
		<form  id="signupForm" class="form-horizontal" name="formName" role="form">
			<input type="hidden" name="id" id="id" />
			<div class="form-group">
				<label for="name">名称:</label> <input class="form-control" id="name" name="name" required />
			</div>

			<div class="form-group">
				<label for="name">密码:</label> <input class="form-control" id="password" name="password" />
			</div>
            
            
    [#list rlist as role]
        <label class="checkbox-inline">
          <input type="checkbox" id="role_${role.id}"  name="adminRoles[${role_index}].role.id"  value="${role.id}"> ${role.roleName}
        </label>
    [/#list]
        
        
			<div class="form-group" style="margin-top: 10px;"  >
				<div style="text-align: center;">
				   <button  onclick="sub();"  type="button" class="btn btn-info">提交</button>
				</div>
			</div>
		</form>
	</div>

</body>
</html>